# -*- coding: utf-8 -*-
import os
import wx
from datetime import datetime
from apps.models import Event as EventModel
if os.name != 'posix':
    import pyTTS

class TimerWindow(wx.Window):
    def __init__(self, parent):
        print 'init Timer Window'
        wx.Window.__init__(self, parent)
        self.frame = parent
        self.timer = wx.Timer(self)
        if os.name != 'posix':
            self.tts = pyTTS.Create()
            self.tts.SetVoiceByName('MSSimplifiedChineseVoice')        
        self.Bind(wx.EVT_TIMER, self.OnTime, self.timer)
#        self.timer.Start(1000)
        
    def OnTime(self, event):
        now = datetime.now()
        self.frame.statusBar1.SetStatusText(now.strftime("%Y-%m-%d %H:%M:%S"), 3)
        try:
            now = now.strptime(now.strftime("%Y-%m-%d-%H:%M:%S"), "%Y-%m-%d-%H:%M:%S")
            latest_event = EventModel.objects.filter(eventtime__gte=now).order_by("eventtime")[0]
            print "event time: %s" % latest_event.eventtime
            self.frame.statusBar1.SetStatusText("event time: %s" % latest_event.eventtime, 2)
            print str(now.strftime("%Y-%m-%d-%H:%M:%S"))
            if str(now.strftime("%Y-%m-%d-%H:%M:%S")) == \
                str(latest_event.eventtime.strftime("%Y-%m-%d-%H:%M:%S")):
                print "the event %s will be platforms" % latest_event.eventtime
                wx.CallAfter(self.frame.run)
                if os.name != 'posix':
                    self.tts.Speak(latest_event.description)                
        except IndexError:
            print 'no events'
            self.frame.statusBar1.SetStatusText("no events", 2)
